package org.mx.service.server;

import java.util.EnumSet;

import javax.servlet.DispatcherType;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.mx.service.server.jmx.WebServer;
import org.mx.service.web.MyWebServerCrossFilter;
import org.springframework.context.ApplicationContext;

/**
 * 描述： 基于Jetty的Web服务的工厂类
 * 
 * @author john.peng
 *
 */
public class WebServerFactory extends HttpServerFactory {
	private WebServerConfigBean webServerConfigBean;

	/**
	 * 构造函数
	 * @param context Spring IoC容器上下文
	 * @param webServerConfigBean Web服务器配置信息实体
	 * @param webServerMXBean	Web服务器MXBean对象
	 */
	public WebServerFactory(ApplicationContext context, WebServerConfigBean webServerConfigBean,
			WebServer webServerMXBean) {
		super(context, webServerConfigBean, webServerMXBean);
		this.webServerConfigBean = webServerConfigBean;
	}

	@Override
	protected Handler getHandler() {
		WebAppContext context = new WebAppContext();
		context.setContextPath(webServerConfigBean.getContextPath());
		context.setConfigurationDiscovered(true);
		context.setClassLoader(Thread.currentThread().getContextClassLoader());
		context.setResourceBase(webServerConfigBean.getResourceBase());
		FilterHolder holder = new FilterHolder(MyWebServerCrossFilter.class);
		context.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
		return context;
	}
}
